package com.ctgu.tengyun.controller.customer;

import com.ctgu.tengyun.bean.convert.CustomerConvert;
import com.ctgu.tengyun.bean.errorcode.CommonErrorCode;
import com.ctgu.tengyun.bean.response.CustomerListResponse;
import com.ctgu.tengyun.domain.CustomerInfo;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.customer.CustomerInfoService;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/7.
 */
@RestController
@RequestMapping(value = "/v1/customer/")
public class CustomerController {

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping(value = "save_or_update")
    public Object saveOrUpdate(@RequestBody CustomerInfo request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getContactNo()), "contactNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getName()), "name is required.");

        if (null != request.getId()) {
            CustomerInfo customerInfo = customerInfoService.selectByPrimaryKey(request.getId());
            Validator.assertResourceFound(null != customerInfo, "customer not found, id:" + request.getId());
            request.setVersion(customerInfo.getVersion());

            customerInfoService.updateByPrimaryKeySelective(request);
        } else {

            try {
                customerInfoService.insertSelective(request);
            } catch (DuplicateKeyException e) {
                throw new ErrorCodeException(CommonErrorCode.duplicate_add, CommonErrorCode.duplicate_add.getMsg() + request.getContactNo());
            }
        }

        return ImmutableMap.of("id", request.getId());
    }

    @RequestMapping(value = "list")
    public CustomerListResponse list(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String customerName = ParameterTool.getParameterString(request, "name", null);
        String contactNo = ParameterTool.getParameterString(request, "contactNo", null);

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        CustomerInfo queryInfo = new CustomerInfo();
        queryInfo.setName(customerName);
        queryInfo.setContactNo(contactNo);
        Integer totalCount = customerInfoService.countCustomer(queryInfo);
        List<CustomerInfo> customerInfoList = Collections.emptyList();
        if (totalCount > 0) {
            customerInfoList = customerInfoService.selectCustomerList(queryInfo, pageNo, pageSize);
        }

        return CustomerConvert.toCustomerListResponse(totalCount, pageSize, customerInfoList);
    }

    @GetMapping(value = "detail/{id}")
    public CustomerInfo detail(@PathVariable("id") Long customerId) {
        CustomerInfo customerInfo = customerInfoService.selectByPrimaryKey(customerId);
        Validator.assertResourceFound(null != customerInfo, "customer not found, id:" + customerId);

        return customerInfo;
    }
}
